/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : May 25, 2012  12:20:56 PM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : apps.business.process.sales.customerRequest
 * File Name : CustomerRequest.java
 * TODO
 */
package apps.business.process.sales.customerRequest;

import java.util.ArrayList;
import java.util.List;
import jpa.entitys.common.RcvdNoPaymentType;
import jpa.entitys.promis.common.VwSyncRcvdInstallment;
import jpa.entitys.synchro.staging.FiArreceiptreOutb;
import jpa.entitys.synchro.staging.PsWbsMaster;
import jpa.entitys.synchro.staging.SdBookContrOutb;
import jpa.entitys.synchro.staging.SdChargecode;
import org.apache.log4j.Logger;
import promis.utility.CommonService;
import promis.utility.Utilitys;
import sap.business.fi.outbound.FiArreceiptreOutbService;
import sap.business.ps.outbound.PsWbsMasterService;
import sap.business.sd.outbound.SdChargecodeService;
import apps.business.process.sales.booking.SalesBookingProcessing;
import apps.promis.out.common.PropertyUnitAction;
import apps.promis.out.common.VwSyncRcvdInstallmentAction;
import apps.promis.out.sales.ReceiptedChargeCodeAction;

/**
 * @author chatree
 *
 */
public class CustomerRequest {
	
	private static final Logger log = Logger.getLogger(CustomerRequest.class);

	/**
	 * 
	 */
	public CustomerRequest() {
		// TODO Auto-generated constructor stub
	}
	
	
	
	@Deprecated
	/**
	 * 		String company = "SP";
			String project = "SPLCITY-SRI";
			String BookingOrContarctNo = "BS-1205-000015"; //  "CS-1204-000009-0" // "BS-1204-000007"; //BS-1204-000016
			String ReceipteddocumentNo = "RR-1205-000059";
			String status = "A";
			String syncIsstaging = "N"; //Can set value to [null]
			String issync = "N";
	 * @param company
	 * @param project
	 * @param BookingOrContarctNo
	 * @param ReceipteddocumentNo
	 * @param status
	 * @param syncIsstaging
	 * @param issync
	 * @throws Exception
	 */
	public void CancelBooking(String company, String project, String BookingOrContarctNo, String ReceipteddocumentNo, String status, String syncIsstaging, String issync) throws Exception {
		try{
		
			SalesBookingProcessing obj = new SalesBookingProcessing();
			String buildingPhase = null;
			String floorZone  = null;
			String unitNumber  = null;
			String instanceNo = null;
			
			List<SdBookContrOutb> bookinglst = obj.newProcessingSingle(company, project, BookingOrContarctNo, status, syncIsstaging, issync);
			
			if(!CommonService.IsNull(bookinglst)){
				instanceNo = bookinglst.get(0).getInstanceNo(); 				log.info("instanceNo = " + instanceNo);
				floorZone = bookinglst.get(0).getFloorZone();					log.info("floorZone = " + floorZone);
				buildingPhase = bookinglst.get(0).getBuildingPhaseCode();		log.info("buildingPhase = " + buildingPhase);
				unitNumber = bookinglst.get(0).getUnitNumber();					log.info("unitNumber  = " + unitNumber);
			}
			
			
			//Property Unit///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
			PropertyUnitAction propertyunit = new PropertyUnitAction();
			propertyunit.setInstanceNo(instanceNo);
			List<PsWbsMaster> unitLst = propertyunit.CreatedToStaging(company, project, buildingPhase, floorZone, unitNumber);
			log.info("Create Property Unit to from BS,CS Staging    ============================================>");
			
			new PsWbsMasterService().SendingToSAP(unitLst);
			log.info("Sending Property Unit "+ unitLst.get(0).getUnitNumber() +" from BS,CS to SAP       ============================================>");
			
			//Receipted ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////				
			if(!CommonService.IsNull(ReceipteddocumentNo)){

				VwSyncRcvdInstallmentAction vwSyncRcvdInstallmentAction = new VwSyncRcvdInstallmentAction();
				vwSyncRcvdInstallmentAction.setInstanceNo(instanceNo);
				List<FiArreceiptreOutb> rcvdLst = vwSyncRcvdInstallmentAction.CreateRcvdToStaging(company, project, ReceipteddocumentNo, status, syncIsstaging);
				log.info("Create Receipted  to Staging  ============================================>");
				
				//Create ChargeCode To Staging
				ReceiptedChargeCodeAction receiptedChargeCodeAction = new ReceiptedChargeCodeAction();
				receiptedChargeCodeAction.setInstanceNo(instanceNo);
			    List<SdChargecode> newValueList = receiptedChargeCodeAction.CreateToStaging(rcvdLst);
				
				//Sending ChargeCode to SAP
				log.info("Start Sending Rcvd ChargeCode To Staging ==========================================>");
				SdChargecodeService SdChargecodeServ = new SdChargecodeService();
				SdChargecodeServ.SendingToSAP(newValueList);
				log.info("End Sending Rcvd ChargeCode To Staging ============================================>");

				//Receipted----------------------------Send to SAP----------------------------------
				new FiArreceiptreOutbService().SendingToSAP(rcvdLst);
				log.info("Sending Receipted : " + rcvdLst.get(0).getReceiptDocNo() + " to SAP ============================================>");				
			}
			
		}catch(Exception e)
		{
			log.error(e);
		}
	}
	
	@Deprecated
	/**
	 * 		String company = "SP";
			String project = "SPLCITY-SRI";
			String BookingOrContarctNo = "CS-1205-000011"; //  "CS-1204-000009-0" // "BS-1204-000007"; //BS-1204-000016
			String ReceipteddocumentNo = "RR-1205-000061";
			String status = "A";
			String syncIsstaging = "N"; //Can set value to [null]
			String issync = "N";
	 * @param company
	 * @param project
	 * @param BookingOrContarctNo
	 * @param ReceipteddocumentNo
	 * @param status
	 * @param syncIsstaging
	 * @param issync
	 * @throws Exception
	 */
	public void CancelContract(String company, String project, String BookingOrContarctNo, String ReceipteddocumentNo, String status, String syncIsstaging, String issync) throws  Exception {
		try{
			
			SalesBookingProcessing obj = new SalesBookingProcessing();
			String buildingPhase = null;
			String floorZone  = null;
			String unitNumber  = null;
			String instanceNo = null;
			
			List<SdBookContrOutb> bookinglst = obj.newProcessingSingle(company, project, BookingOrContarctNo, status, syncIsstaging, issync);

			if(!CommonService.IsNull(bookinglst)){
				instanceNo = bookinglst.get(0).getInstanceNo(); 				log.info("instanceNo = " + instanceNo);
				floorZone = bookinglst.get(0).getFloorZone();					log.info("floorZone = " + floorZone);
				buildingPhase = bookinglst.get(0).getBuildingPhaseCode();		log.info("buildingPhase = " + buildingPhase);
				unitNumber = bookinglst.get(0).getUnitNumber();					log.info("unitNumber  = " + unitNumber);
			}
			
			
			//Property Unit///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
			PropertyUnitAction propertyunit = new PropertyUnitAction();
			propertyunit.setInstanceNo(instanceNo);
			List<PsWbsMaster> unitLst = propertyunit.CreatedToStaging(company, project, buildingPhase, floorZone, unitNumber);
			log.info("Create Property Unit to from BS,CS Staging    ============================================>");
			
			new PsWbsMasterService().SendingToSAP(unitLst);
			log.info("Sending Property Unit "+ unitLst.get(0).getUnitNumber() +" from BS,CS to SAP       ============================================>");
			
			
			//Receipted ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////				
			if(!CommonService.IsNull(ReceipteddocumentNo)){

				VwSyncRcvdInstallmentAction vwSyncRcvdInstallmentAction = new VwSyncRcvdInstallmentAction();
				vwSyncRcvdInstallmentAction.setInstanceNo(instanceNo);
				List<FiArreceiptreOutb> rcvdLst = vwSyncRcvdInstallmentAction.CreateRcvdToStaging(company, project, ReceipteddocumentNo, status, syncIsstaging);
				
				//Create ChargeCode To Staging
				ReceiptedChargeCodeAction receiptedChargeCodeAction = new ReceiptedChargeCodeAction();
				receiptedChargeCodeAction.setInstanceNo(instanceNo);
			    List<SdChargecode> newValueList = receiptedChargeCodeAction.CreateToStaging(rcvdLst);
				
				SdChargecodeService SdChargecodeServ = new SdChargecodeService();
				SdChargecodeServ.SendingToSAP(newValueList);
				log.info("Sending Rcvd ChargeCode To Staging ============================================>");

				//Receipted----------------------------Send to SAP----------------------------------
				new FiArreceiptreOutbService().SendingToSAP(rcvdLst);
				log.info("Sending Receipted : " + rcvdLst.get(0).getReceiptDocNo() + " to SAP ============================================>");				
			}
			
		}catch(Exception e){			
			log.error(e.getMessage());
			System.err.println(e.getMessage());
			throw e;
		}
	}
	

	/**
	 * 		String company = "SP";
			String project = "SPLVILLE-PRAGSA"; //SPLCITY-SRI
			String BookingOrContarctNo = "CS-1206-000006-0"; //  "CS-1204-000009-0" // "BS-1204-000007"; //BS-1204-000016
			String status = "Z";
			String syncIsstaging = null; //Can set value to [null]
			String issync = "N";
	 * @param company
	 * @param project
	 * @param BookingOrContarctNo
	 * @param status
	 * @param syncIsstaging
	 * @param issync
	 * @throws Exception
	 */
	public void RequestForCancelContract(String company, String project, String BookingOrContarctNo, String status, String syncIsstaging, String issync) throws Exception{
		try{
			
			SalesBookingProcessing obj = new SalesBookingProcessing();
			/*
			String buildingPhase = null;
			String floorZone  = null;
			String unitNumber  = null;
			String instanceNo = null;
			*/
			
			List<SdBookContrOutb> bookinglst = obj.newProcessingSingle(company, project, BookingOrContarctNo, status, syncIsstaging, issync);
			
			/*
			if(!CommonService.IsNull(bookinglst)){
				instanceNo = bookinglst.get(0).getInstanceNo(); 				log.info("instanceNo = " + instanceNo);
				floorZone = bookinglst.get(0).getFloorZone();					log.info("floorZone = " + floorZone);
				buildingPhase = bookinglst.get(0).getBuildingPhaseCode();		log.info("buildingPhase = " + buildingPhase);
				unitNumber = bookinglst.get(0).getUnitNumber();					log.info("unitNumber  = " + unitNumber);
			}
			
			//Property Unit///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
			PropertyUnitAction propertyunit = new PropertyUnitAction();
			propertyunit.setInstanceNo(instanceNo);
			List<PsWbsMaster> unitLst = propertyunit.CreatedToStaging(company, project, buildingPhase, floorZone, unitNumber);
			log.info("Create Property Unit to from BS,CS Staging    ============================================>");
			
			new PsWbsMasterService().SendingToSAP(unitLst);
			log.info("Sending Property Unit "+ unitLst.get(0).getUnitNumber() +" from BS,CS to SAP       ============================================>");
			*/
			
		}catch(Exception e)
		{
			log.error(e.getMessage());
			System.err.println(e.getMessage());
			throw e;
		}
	}
	
	/**
	 * 		String company = "SP";
			String project = "SPLVILLE-PRAGSA"; //SPLCITY-SRI
			String BookingOrContarctNo = "BS-1206-000009"; //  "CS-1204-000009-0" // "BS-1204-000007"; //BS-1204-000016
			String status = "Z";
			String syncIsstaging = null; //Can set value to [null]
			String issync = "N";
	 * @param company
	 * @param project
	 * @param BookingOrContarctNo
	 * @param status
	 * @param syncIsstaging
	 * @param issync
	 * @throws Exception
	 */
	public void RequestForCancelBooking(String company, String project, String BookingOrContarctNo, String status, String syncIsstaging, String issync) throws Exception{
		try{
			
			SalesBookingProcessing obj = new SalesBookingProcessing();
			/*
			String buildingPhase = null;
			String floorZone  = null;
			String unitNumber  = null;
			String instanceNo = null;
			*/
			
			List<SdBookContrOutb> bookinglst = obj.newProcessingSingle(company, project, BookingOrContarctNo, status, syncIsstaging, issync);
			
			/*
			if(!CommonService.IsNull(bookinglst)){
				instanceNo = bookinglst.get(0).getInstanceNo(); 				log.info("instanceNo = " + instanceNo);
				floorZone = bookinglst.get(0).getFloorZone();					log.info("floorZone = " + floorZone);
				buildingPhase = bookinglst.get(0).getBuildingPhaseCode();		log.info("buildingPhase = " + buildingPhase);
				unitNumber = bookinglst.get(0).getUnitNumber();					log.info("unitNumber  = " + unitNumber);
			}
			
			
			//Property Unit///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
			PropertyUnitAction propertyunit = new PropertyUnitAction();
			propertyunit.setInstanceNo(instanceNo);
			List<PsWbsMaster> unitLst = propertyunit.CreatedToStaging(company, project, buildingPhase, floorZone, unitNumber);
			log.info("Create Property Unit to from BS,CS Staging    ============================================>");
			
			new PsWbsMasterService().SendingToSAP(unitLst);
			log.info("Sending Property Unit "+ unitLst.get(0).getUnitNumber() +" from BS,CS to SAP       ============================================>");
			*/
			
		}catch(Exception e)
		{
			log.error(e.getMessage());
			System.err.println(e.getMessage());
			throw e;
		}
	}
	
	/**
	 * GenerateChargeCode from old sales booking or contract
	 * @param company
	 * @param project
	 * @param CN_DocumentNo
	 * @param refDocNo
	 * @param status
	 * @param From_project
	 * @return
	 * @throws Exception
	 */
	private List<SdChargecode> GenerateChargeCode (String company, String project, String fromProject, String CN_DocumentNo, String refDocNo, String status, String syncIsstaging_opt) throws Exception {
		StringBuilder _strQuery = new StringBuilder();
		ArrayList<SdChargecode> valuelist =  null;
		try{
						
			VwSyncRcvdInstallmentAction acvdInstallment = new VwSyncRcvdInstallmentAction();
			
			//Depicate Dr.Tree
			//List<VwSyncRcvdInstallment> old_acvd_installment = acvdInstallment.findNoPaymentTypeByRefDocNo(value.getCompany(), value.getProject(), value.getRef_doc_no(), status, syncIsstaging);
			
			_strQuery = new StringBuilder();
			_strQuery.append(" SELECT c FROM VwSyncRcvdInstallment c "); 
			_strQuery.append(" WHERE c.company = ").append(Utilitys.strSql(company));
			_strQuery.append(" And c.project = ").append(Utilitys.strSql(project));
			_strQuery.append(" And c.fromProject = ").append(Utilitys.strSql(fromProject));
			_strQuery.append(" And c.documentNo = ").append(Utilitys.strSql(CN_DocumentNo));
			_strQuery.append(" And c.refDocNo = ").append(Utilitys.strSql(refDocNo));
			_strQuery.append(" And c.status = ").append(Utilitys.strSql(status));
			
			if(!CommonService.IsNull(syncIsstaging_opt))
				_strQuery.append(" And c.syncIsstaging = ").append(Utilitys.strSql(syncIsstaging_opt));
			
			_strQuery.append(" and c.paymentType is null ");
			_strQuery.append(" order by c.company asc,c.project asc,c.refDocNo asc,c.chargeCode asc");
					
			log.info(CommonService.ReplaceStringToTxtDatabase(_strQuery.toString()));
			
			List<VwSyncRcvdInstallment> old_acvd_installment = acvdInstallment.FindUnitInInstallment1(_strQuery.toString());
			log.info("Charge Code only old_acvd_installment.size() = " + CommonService.getSize(old_acvd_installment));
			
			if(!CommonService.IsNull(old_acvd_installment))
			{
				valuelist = new ArrayList<SdChargecode>();
				for(VwSyncRcvdInstallment vlaueTemp : old_acvd_installment){
					
					if(!CommonService.IsNull(vlaueTemp.getChargeCode())){
						SdChargecode model = new SdChargecode();
						
						model.setChargeCode(vlaueTemp.getChargeCode());
						model.setDocumentNo(refDocNo);
						model.setCompany(company);
						model.setSapCompanyCode(vlaueTemp.getSapCompanyCode());
						
						//************************************************
						//ORI model.setProject(value.getProject());
						//Lasted Edit 2012-09-19 //DR.TREE										
						model.setProject(vlaueTemp.getFromProject());
						//************************************************
						
						//************************************************
						//ORI model.setSapPlantCode(vlaueTemp.getSapPlantCode()); 
						//Lasted Edit 2012-09-19 //DR.TREE
						model.setSapPlantCode(vlaueTemp.getFromSapPlantCode());
						//************************************************
						
						model.setRefReceiptedNo(vlaueTemp.getDocumentNo());
						model.setInterfaceNo("SDI07");																				
						model.setCurrency(vlaueTemp.getCurrency());
						model.setStatus(vlaueTemp.getStatus());
						model.setReceivedDate(CommonService.ConvertDateToSAPStringDate(vlaueTemp.getReceiptDate()));
						model.setReceivedAmt(CommonService.IsNullRZero(vlaueTemp.getAmountReceived()));
						model.setRefundAmt(CommonService.IsNullRZero(vlaueTemp.getTotRefund()));

						model.setCreatedBy(vlaueTemp.getCreatedBy());
						model.setCreatedOn(CommonService.ConvertDateToSAPStringDate(vlaueTemp.getCreatedOn()));
						model.setUpdatedBy(vlaueTemp.getLastUpdBy());
						model.setUpdatedOn(CommonService.ConvertDateToSAPStringDate(vlaueTemp.getLastUpdOn()));
						
						if(CommonService.IsNull(vlaueTemp.getLastUpdBy())){
							model.setUpdatedBy(vlaueTemp.getCreatedBy());
							model.setUpdatedOn(CommonService.ConvertDateToSAPStringDate(vlaueTemp.getCreatedOn()));
						}
						
						valuelist.add(model);
					}
				}
			}
			
			return valuelist;
			
		}catch(Exception e){			
			log.error(e.getMessage());
			System.err.println(e.getMessage());
			throw e;
		}
	}

	
	/**
	 * 		String company = "SP";
			String project = "SPLVILLE-PRAGSA";
			String CN_DocumentNo = "CN-1206-000033";
			String status = "A";
			String syncIsstaging = "Y";
	 * @param company
	 * @param project
	 * @param CN_DocumentNo
	 * @param status
	 * @param syncIsstaging
	 * @throws Exception
	 */
	public void RequestForMoveUnitToBookOrContract(String company, String project, String CN_DocumentNo, String status, String syncIsstaging_opt) throws Exception {
		try{
			
			SalesBookingProcessing salesProcessing = new SalesBookingProcessing();
			VwSyncRcvdInstallmentAction acvdInstallment = new VwSyncRcvdInstallmentAction();
			
			try{
				
				//1. Send to SDI03 + SDI07 (OLD) 
				StringBuilder _strQuery = new StringBuilder();

				//New or To Sales Booking Or Contract
				_strQuery = new StringBuilder();
				_strQuery.append(" SELECT  ");
				_strQuery.append(" A.DOCUMENT_NO,  ");
				_strQuery.append(" A.REF_CONTRACT_NO, ");
				_strQuery.append(" B.BUILDING_PHASE_CODE, ");
				_strQuery.append(" B.FLOOR_ZONE_CODE, ");
				_strQuery.append(" B.UNIT_NUMBER, ");
				_strQuery.append(" B.STATUS ");
				_strQuery.append(" FROM VW_SYNC_RCVD_CN_NEW_OLD A ");
				_strQuery.append(" INNER JOIN VW_SYNC_MKT_CUST_BOOK_CONT B ON (B.DOCUMENT_NUMBER = A.REF_CONTRACT_NO AND A.COMPANY = B.COMPANY AND A.PROJECT = B.PROJECT) ");
				_strQuery.append(" WHERE A.COMPANY = ").append(Utilitys.strSql(company));
				_strQuery.append(" AND A.PROJECT = ").append(Utilitys.strSql(project));
				_strQuery.append(" AND A.DOCUMENT_NO = ").append(Utilitys.strSql(CN_DocumentNo));
				_strQuery.append(" AND A.STATUS = ").append(Utilitys.strSql(status));
				_strQuery.append(" GROUP BY  ");
				_strQuery.append(" A.DOCUMENT_NO, ");
				_strQuery.append(" A.REF_CONTRACT_NO, ");
				_strQuery.append(" B.BUILDING_PHASE_CODE, ");
				_strQuery.append(" B.FLOOR_ZONE_CODE, ");
				_strQuery.append(" B.UNIT_NUMBER, ");
				_strQuery.append(" B.STATUS ");
				log.info(CommonService.ReplaceStringToTxtDatabase(_strQuery.toString()));
				List<Object[]> result = acvdInstallment.FindUnitInInstallment(_strQuery.toString());
				
				
				//From Sales Booking Or Contract
				_strQuery = new StringBuilder();
				_strQuery.append(" SELECT  "); 
				_strQuery.append(" A.DOCUMENT_NO,  ");
				_strQuery.append(" A.REF_DOC_NO,  ");
				_strQuery.append(" B.BUILDING_PHASE_CODE,  ");
				_strQuery.append(" B.FLOOR_ZONE_CODE,  ");
				_strQuery.append(" B.UNIT_NUMBER, ");
				_strQuery.append(" B.STATUS ");
				_strQuery.append(" FROM VW_SYNC_RCVD_CN_NEW_OLD A  ");
				_strQuery.append(" INNER JOIN VW_SYNC_MKT_CUST_BOOK_CONT B ON (B.DOCUMENT_NUMBER = A.REF_DOC_NO AND A.COMPANY = B.COMPANY AND A.PROJECT = B.PROJECT)  ");
				_strQuery.append(" WHERE A.COMPANY = ").append(Utilitys.strSql(company));
				_strQuery.append(" AND A.PROJECT = ").append(Utilitys.strSql(project));
				_strQuery.append(" AND A.DOCUMENT_NO = ").append(Utilitys.strSql(CN_DocumentNo));
				_strQuery.append(" AND A.STATUS = ").append(Utilitys.strSql(status));
				_strQuery.append(" GROUP BY A.DOCUMENT_NO, ");
				_strQuery.append(" A.REF_DOC_NO, ");
				_strQuery.append(" B.BUILDING_PHASE_CODE, ");
				_strQuery.append(" B.FLOOR_ZONE_CODE, ");
				_strQuery.append(" B.UNIT_NUMBER, ");
				_strQuery.append(" B.STATUS ");
				log.info(CommonService.ReplaceStringToTxtDatabase(_strQuery.toString()));
				List<Object[]> result1 = acvdInstallment.FindUnitInInstallment(_strQuery.toString());
				
				
				List<Object[]> newList = new ArrayList<Object[]>();
				if(result != null){
					for(Object[] resultElement : result)
						newList.add(resultElement);
				}
								
				if(result1 != null){
					for(Object[] resultElement : result1)
						newList.add(resultElement);
				}
				
				
				//*************************************************************************************************
				//Sending New Sales Booking Or Sales Contract 
				if(!CommonService.IsNull(result)){
					
					log.info("result.size = " + newList.size());
					 for (Object[] resultElement : newList) {
						 String sales_destination = CommonService.IsNullRString(resultElement[1]); 				log.info(" sales_destination = " + sales_destination);
						 String sales_status = CommonService.IsNullRString(resultElement[5]);					log.info(" sales_status = " + sales_status);
						 
						 List<SdBookContrOutb> lstBooking = salesProcessing.newProcessingSingle(company, project, sales_destination, sales_status, null, "N");
						 if(!CommonService.IsNull(lstBooking))
							 log.info("Sendind Sales Booking or Contract [Company="+ lstBooking.get(0).getCompany() + " ||  project=" + lstBooking.get(0).getProject() + " || sales_destination=" + lstBooking.get(0).getDocumentNumber()  + " || sales_status=" + lstBooking.get(0).getStatus() +" || Staging= "+ lstBooking.get(0).getInstanceNo() + " ] to SAP.");
						 else
							 log.info("Find not Found Sales Booking or Contract [company="+ company + " ||  project=" + project + " || sales_destination=" + sales_destination  + " || sales_status=" + sales_status +"] to SAP.");
						 
						 //Property Unit
						 String buildingPhase = CommonService.IsNullRString(resultElement[2]);					log.info("buildingPhase = " + buildingPhase);
						 String floorZone  = CommonService.IsNullRString(resultElement[3]);						log.info("floorZone = " + floorZone);						
						 String unitNumber  = CommonService.IsNullRString(resultElement[4]);					log.info("unitNumber = " + unitNumber);						
						 List<PsWbsMaster> unitLst = new PropertyUnitAction().CreatedToStaging(company, project, buildingPhase, floorZone, unitNumber);						
						 log.info("Created Property Unit "+ company + "||" + project + "||" + unitLst.get(0).getBuildingPhase() + "||" + unitLst.get(0).getFloorZone() + "||" + unitLst.get(0).getUnitNumber() +" from BS,CS to Staging --->");
						 
						 new PsWbsMasterService().SendingToSAP(unitLst);
						 log.info("Sending Property Unit "+ company + "||" + project + "||" + unitLst.get(0).getBuildingPhase() + "||" + unitLst.get(0).getFloorZone() + "||" + unitLst.get(0).getUnitNumber() +" from BS,CS to SAP --->");
					 }
				}
				//*************************************************************************************************
				
				
				//Charge Code only 
				//Rcvd for No PaymentType
				//*************************************************************************************************
				List<RcvdNoPaymentType> NoPaymentTypeList = acvdInstallment.FindUnitNoPaymentType(company, project, CN_DocumentNo, status, syncIsstaging_opt);
				if(NoPaymentTypeList != null){
					if(NoPaymentTypeList.size() > 0)
					{						
						for(RcvdNoPaymentType value : NoPaymentTypeList){  //Per OLD Contract or Unit
							log.info("FromProject=" + value.getFrom_project() +" || FromDocument=" + value.getRef_doc_no() + " || FromUnit=" + value.getFrom_unit_number());
																																	
							try{
								
								//From or OLD  Sales Booking or Sales Contract 
								String BookingOrContarctNo = value.getRef_doc_no();
								String from_project = value.getFrom_project();
								log.info("Searching sales txn from or old sales contract or booking [company="+ company +" || from_project="+ from_project + " || BookingOrContarctNo="+ BookingOrContarctNo + " || Status=X] --->");
								List<SdBookContrOutb> bookinglst = salesProcessing.newProcessingSingle(company, from_project, BookingOrContarctNo, "X", null, "N");
								if(!CommonService.IsNull(bookinglst))
									log.info("Sending from or old sales contract or booking [company="+ bookinglst.get(0).getCompany() +" || from_project="+ bookinglst.get(0).getProject() + " || BookingOrContarctNo="+ bookinglst.get(0).getDocumentNo() + " || Status=X || InstanceNo="+ bookinglst.get(0).getInstanceNo() + "] --->");
								else
									log.warn("Find not found sales txn from or old sales contract or booking [company="+ company +" || from_project="+ from_project + " || BookingOrContarctNo="+ BookingOrContarctNo + " || Status=X] --->");	
								
								/*
								//Property Unit
								String buildingPhase = value.getFrom_building_phase_code();
								String floorZone  = value.getFrom_floor_zone_code();			
								String unitNumber  = value.getFrom_unit_number();
								List<PsWbsMaster> unitLst = new PropertyUnitAction().CreatedToStaging(company, project, buildingPhase, floorZone, unitNumber);
								log.info("Create Property Unit to from BS,CS Staging    ============================================>");
								
								new PsWbsMasterService().SendingToSAP(unitLst);
								log.info("Sending Property Unit "+ unitLst.get(0).getUnitNumber() +" from BS,CS ["+ bookinglst.get(0).getDocumentNo() +"] to SAP       ============================================>");
							    */
								
								//******************************************************************************
								//Create ChargeCode To Staging
								log.info("Start Calurate Charge [Company=" + company +" || Project=" + project + " || from_project=" + from_project +" || CN_DocumentNo=" + CN_DocumentNo +" || Ref_doc_no=" + value.getRef_doc_no() +" || status=" + status + "] --->");
								List<SdChargecode> valuelist = GenerateChargeCode(company, project, from_project, CN_DocumentNo, value.getRef_doc_no(), status, syncIsstaging_opt);
								log.info("Calurate Charge [Company=" + company +" || Project=" + project + " || from_project=" + from_project +" || CN_DocumentNo=" + CN_DocumentNo +" || Ref_doc_no=" + value.getRef_doc_no() +" || status=" + status + "] Size="+ CommonService.getSize(valuelist) +" --->");
								
								if(!CommonService.IsNull(valuelist)){																		
									List<SdChargecode> chargeCodeList = new ReceiptedChargeCodeAction().CreateToStaging(valuelist);
								    log.info("Create Charge Code [Company=" + chargeCodeList.get(0).getCompany() +" || Project=" + chargeCodeList.get(0).getProject() + " || DocumentNo=" + chargeCodeList.get(0).getDocumentNo() +" || ChargeCode=" + chargeCodeList.get(0).getChargeCode() +" || InstanceNo=" + chargeCodeList.get(0).getInstanceNo() +"] to Staging --->");
								    
									//Sending ChargeCode to SAP (SDI07)									
									new SdChargecodeService().SendingToSAP(chargeCodeList);								
									log.info("Sending Charge Code [Company=" + chargeCodeList.get(0).getCompany() +" || Project=" + chargeCodeList.get(0).getProject() + " || DocumentNo=" + chargeCodeList.get(0).getDocumentNo() +" || ChargeCode=" + chargeCodeList.get(0).getChargeCode() +" || InstanceNo=" + chargeCodeList.get(0).getInstanceNo() +"] to Staging --->");
								}else
									log.warn("Can't Calurate Charge [ company=" + company +" || project=" + project + " || from_project=" + from_project +" || CN_DocumentNo=" + CN_DocumentNo +" || Ref_doc_no=" + value.getRef_doc_no() +" || status=" + status + "] --->");
							    
								//******************************************************************************
								
							}catch(Exception e)
							{
								log.error(e.getMessage());
								System.err.println(e.getMessage());
								throw e;
							}
						}
					}
				}
				
			}catch(Exception e){
				log.error(" Exception Case OLD Unit.");
				log.error(e.getMessage());
				System.err.println(e.getMessage());
				throw e;
			}
			
			
			//2. Send to FI-T1-050 (NEW)
			//CN for New Contact or Booking
			try{
				log.info("Searching Sales Booking or Contract destination to [company="+ company +" || project=" + project +" || CN_DocumentNo="+ CN_DocumentNo +" || status="+ status +" || syncIsstaging_opt="+ syncIsstaging_opt +"] --->");
				List<VwSyncRcvdInstallment> acvdNewContractlst = acvdInstallment.findHasPaymentTypeByDocumentNo(company, project, CN_DocumentNo, status, syncIsstaging_opt);
				if(!CommonService.IsNull(acvdNewContractlst)){								
					try{											
						
						VwSyncRcvdInstallment rcvdInst = acvdNewContractlst.get(0);
						log.info("Have Sales Booking or Contract destination to [company="+ rcvdInst.getCompany() +" || project=" + rcvdInst.getProject() +" || CN_DocumentNo="+ rcvdInst.getDocumentNo() +" || status="+ rcvdInst.getStatus() +" || SyncIssync="+ rcvdInst.getSyncIssync() +"] --->");
						
						//to Desctination sales Booking or Contract 
						String BookingOrContarctNo = acvdNewContractlst.get(0).getRefContractNo();
						salesProcessing.newProcessingSingle(company, project, BookingOrContarctNo, "A", null, "N");
						//List<SdBookContrOutb> bookinglst = salesProcessing.newProcessingSingle(company, project, BookingOrContarctNo, "A", null, "N");							
						
						/*
						//Property Unit
						String buildingPhase = bookinglst.get(0).getBuildingPhaseCode();
						String floorZone  = bookinglst.get(0).getFloorZone();
						String unitNumber  = bookinglst.get(0).getUnitNumber();
						List<PsWbsMaster> unitLst = new PropertyUnitAction().CreatedToStaging(company, project, buildingPhase, floorZone, unitNumber);
						log.info(" Created Property Unit to from BS,CS Staging    ============================================>");
						
						new PsWbsMasterService().SendingToSAP(unitLst);
						log.info(" Sending Property Unit "+ unitLst.get(0).getUnitNumber() +" from BS,CS ["+ bookinglst.get(0).getDocumentNo() +"] to SAP       ============================================>");
						*/
						
						//Create to CreateRcvdToStaging
						List<FiArreceiptreOutb> CNInstallmentLst = new VwSyncRcvdInstallmentAction().CreateRcvdToStaging(acvdNewContractlst);
						log.info("Create Rcvd Installment To Staging [size= " + CommonService.getSize(CNInstallmentLst) + "] --->");
						if(!CommonService.IsNull(CNInstallmentLst)){
							log.info("Created Rcvd Installment To Staging [size= " + CommonService.getSize(CNInstallmentLst) + " || InstanceNo="+ CNInstallmentLst.get(0).getInstanceNo() +"] --->");
							 
							//Create ChargeCode To Staging 			
						    List<SdChargecode> chargeCodeList = new ReceiptedChargeCodeAction().CreateToStaging(CNInstallmentLst);
						    log.info("Created charge Code Rcvd Installment To Staging [size= " + CommonService.getSize(chargeCodeList) + "] --->");
						    if(!CommonService.IsNull(chargeCodeList)){
						    	log.info("Created charge Code Rcvd Installment To Staging [size= " + CommonService.getSize(chargeCodeList) + " || InstanceNo="+ chargeCodeList.get(0).getInstanceNo() +"] --->");
							    
								//Sending ChargeCode to SAP (SD)
								new SdChargecodeService().SendingToSAP(chargeCodeList);
								log.info("Sending Charge Code [Company=" + chargeCodeList.get(0).getCompany() +" || Project=" + chargeCodeList.get(0).getProject() + " || DocumentNo=" + chargeCodeList.get(0).getDocumentNo() +" || ChargeCode=" + chargeCodeList.get(0).getChargeCode() +" || InstanceNo=" + chargeCodeList.get(0).getInstanceNo() +"] to Staging --->");																							
						    }
						    
						  //Send to RCVD SAP (FI)
							new FiArreceiptreOutbService().SendingToSAP(CNInstallmentLst);
							log.info("Sending CN [Company=" + CNInstallmentLst.get(0).getCompany() +" || Project=" + CNInstallmentLst.get(0).getProject() + " || DocumentNo=" + CNInstallmentLst.get(0).getReceiptDocNo() +" || InstanceNo=" + CNInstallmentLst.get(0).getInstanceNo() +"] to Staging --->");
						}
					}catch(Exception e)
					{
						log.error(e.getMessage());
						System.err.println(e.getMessage());
						throw e;
					}
				}else
					log.warn("Find not Found Sales Booking or Contract destination to [company="+ company +" || project=" + project +" || CN_DocumentNo="+ CN_DocumentNo +" || status="+ status +" || syncIsstaging_opt="+ syncIsstaging_opt +"] --->");
				
			}catch(Exception e){
				log.warn("Exception ==> Find not Found Sales Booking or Contract destination to [company="+ company +" || project=" + project +" || CN_DocumentNo="+ CN_DocumentNo +" || status="+ status +" || syncIsstaging_opt="+ syncIsstaging_opt +"] --->");
				log.error(e.getMessage());
				System.err.println(e.getMessage());
				throw e;
			}
		}catch(Exception e)
		{
			log.error(e.getMessage());
			System.err.println(e.getMessage());;
			throw e;
		}finally{
			log.warn("End RequestForMoveUnitTo Desctination Sales Booking or Contract [company=" + company +" || project="+ project +" || CN_DocumentNo="+ CN_DocumentNo +" || status=" + status +" || syncIsstaging_opt=" + syncIsstaging_opt + "] ---> ");
		}
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		try{
			String company = "SP";
			String project= "OP3-3";
			//String CN_DocumentNo = "CN-1209-000114";
			String BookingOrContarctNo = "CS-1209-000121-0";
			String status = "X";
			String syncIsstaging = "N";
			
			//new CustomerRequest().RequestForMoveUnitToBookOrContract(company, project, CN_DocumentNo, status, syncIsstaging);
			
			new CustomerRequest().RequestForCancelContract(company, project, BookingOrContarctNo, status, syncIsstaging, "N");
			
		}catch(Exception e)
		{
			e.printStackTrace();
			log.error(e);
		}
	}
}
